package com.atlin.project.service.impl;

import com.atlin.project.contant.CommonConstant;
import com.atlin.project.mapper.TempTemplateMapper;

import com.atlin.project.model.domain.TempTemplate;
import com.atlin.project.model.dto.tempTemplate.TempTemplateQueryRequest;
import com.atlin.project.model.vo.TempTemplateVO;
import com.atlin.project.service.TempTemplateService;
import com.atlin.project.utils.sql.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author atlin
 * @description tempTemplate
 * @createDate date
 */
@Service
public class TempTemplateServiceImpl extends ServiceImpl<TempTemplateMapper, TempTemplate>
        implements TempTemplateService {

    @Override
    public void valid(TempTemplate tempTemplate, boolean add) {

    }

    @Override
    public TempTemplateVO getTempTemplateVO(TempTemplate tempTemplate, HttpServletRequest request) {
        TempTemplateVO tempTemplateVO = TempTemplateVO.objToVo(tempTemplate);
        // todo other operation
        return tempTemplateVO;
    }

    /**
     * 获取查询包装类（用户根据哪些字段查询，根据前端传来的请求对象，得到 mybatis 框架支持的查询 QueryWrapper 类）
     *
     * @param tempTemplateQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<TempTemplate> getQueryWrapper(TempTemplateQueryRequest tempTemplateQueryRequest) {
        QueryWrapper<TempTemplate> queryWrapper = new QueryWrapper<>();
        if (tempTemplateQueryRequest == null) {
            return queryWrapper;
        }
        Long id = tempTemplateQueryRequest.getId();
        String title = tempTemplateQueryRequest.getTitle();
        String content = tempTemplateQueryRequest.getContent();
        String sortField = tempTemplateQueryRequest.getSortField();
        String sortOrder = tempTemplateQueryRequest.getSortOrder();

        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
        queryWrapper.like(StringUtils.isNotBlank(content), "content", content);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq("isDelete", false);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);

        // todo other operation
        return queryWrapper;
    }

    @Override
    public Page<TempTemplateVO> getTempTemplateVOPage(Page<TempTemplate> tempTemplatePage, HttpServletRequest request) {
        List<TempTemplate> tempTemplateList = tempTemplatePage.getRecords();
        Page<TempTemplateVO> tempTemplateVOPage = new Page<>(tempTemplatePage.getCurrent(), tempTemplatePage.getSize(), tempTemplatePage.getTotal());
        if (CollectionUtils.isEmpty(tempTemplateList)) {
            return tempTemplateVOPage;
        }
        // todo other operation

        // 填充信息
        List<TempTemplateVO> tempTemplateVOList = tempTemplateList.stream().map(tempTemplate -> {
            TempTemplateVO tempTemplateVO = TempTemplateVO.objToVo(tempTemplate);
            // todo other operation

            return tempTemplateVO;
        }).collect(Collectors.toList());
        tempTemplateVOPage.setRecords(tempTemplateVOList);
        return tempTemplateVOPage;
    }
}




